layui.use(['table', 'layer', 'form', 'laydate', 'util', 'jquery', 'common'], function () {
    let table = layui.table;
    let layer = layui.layer;
    let form = layui.form;
    let laydate = layui.laydate;
    let util = layui.util;
    let $ = layui.$;
    let common = layui.common;


    $("#addBtn").click(function () {
        layer.open({
            type: 1
            , title: '添加餐台信息'
            , area: 'auto'
            , shadeClose: true
            , content: $("#addRegion")
        });
    });
    form.on("submit(addForm)", function (data) {
        $.ajax({
            url: '/table/add_table',
            data: {
                _token: data.field['_token'],
                region_id: data.field['region_id'],
                name: data.field['table_name'],
                remark: data.field['desc'],
                minimum_consumption: data.field['region_min'],
                capacity: data.field['region_capacity'],
            },
            dataType: 'json',
            type: 'post',
            success: function (res) {
                if (res.code === 0){
                    common.tipMessage(res.msg, 6, function () {
                        location.href = '/table/index';
                    })
                }else {
                    common.tipMessage(res.msg)
                }
            },
            error: function (res) {

            },
            before: function () {

            }
        })
        return false;
    });

    $(".hover_boder").click(function () {
        let id = $(this).data('id');
        $.ajax({
            url: '/table/table_info',
            type: 'get',
            data: {
                id: id
            },
            dataType: 'json',
            success: function (res) {
                if (res.code === 0){
                    form.val('tableInfo',{
                        table_name: res.data['name'],
                        region_id: res.data['region_id'],
                        region_sort: res.data['index'],
                        region_capacity: res.data['capacity'],
                        region_min: res.data['minimum_consumption'],
                        desc: res.data['remark'],
                        id: res.data['id']
                    });
                    layer.open({
                        type: 1
                        , title: '修改餐台信息'
                        , area: 'auto'
                        , shadeClose: true
                        , content: $("#editTable")
                    })
                }else {
                    common.tipMessage(res.msg);
                }
            }
        });
    });

    form.on("submit(editForm)", function (data) {
        $.ajax({
            url: '/table/edit_table',
            data: {
                _token: data.field['_token'],
                region_id: data.field['region_id'],
                name: data.field['table_name'],
                remark: data.field['desc'],
                minimum_consumption: data.field['region_min'],
                capacity: data.field['region_capacity'],
                id: data.field['id'],
                index: data.field['region_sort']
            },
            dataType: 'json',
            type: 'post',
            success: function (res) {
                if (res.code === 0){
                    common.tipMessage(res.msg, 6, function () {
                        location.href = '/table/index';
                    })
                }else {
                    common.tipMessage(res.msg)
                }
            },
            error: function (res) {

            },
            before: function () {

            }
        })
        return false;
    });

});

/**
 * 删除餐台
 * @param id
 */
function del(id) {
    layui.use(['common'],function () {
        let $ = layui.$;
        let common = layui.common;
        $.ajax({
            url: '/table/del_table',
            data: {
                id: id,
            },
            type: 'get',
            dataType: 'json',
            success: function (res) {
                if (res.code === 0){
                    common.tipMessage(res.msg, 6, function () {
                        location.href = '/table/index'
                    });
                }else {
                    common.tipMessage(res.msg);
                }
            },
            error: function (res) {

            },
            before: function () {

            }
        });
    })
}

